"use strict";
/*
点击单元格

*/
var win = [
    [0, 1, 2], [3, 4, 5], [6, 7, 8],
    [0, 3, 6], [1, 4, 7], [2, 5, 8],
    [0, 4, 8], [2, 4, 6]
];
var Player;
(function (Player) {
    Player["X"] = "x";
    Player["O"] = "o";
})(Player || (Player = {}));
var currentPlayer;
var steps;
var board = document.querySelector('#board');
var cells = document.querySelectorAll('.cell');
var message = document.querySelector('#message');
var winner = document.querySelector('#winner');
var restart = document.querySelector('#restart');
restartGame();
cells.forEach(function (value, index) {
    var cell = value;
    cell.addEventListener('click', clickCell, { once: true });
});
function clickCell(event) {
    steps++;
    var cell = event.target;
    cell.classList.add(currentPlayer);
    // 判断胜利
    var isWin = checkWin(currentPlayer);
    if (isWin) {
        message.style.display = 'block';
        winner.innerHTML = currentPlayer.toUpperCase() + "赢了!";
        return;
    }
    if (steps === 9) {
        message.style.display = 'block';
        winner.innerHTML = "平局";
        return;
    }
    // 切换到下一名玩家
    currentPlayer = currentPlayer === Player.X ? Player.O : Player.X;
    // 提示
    var gameBoard = document.querySelector('#board');
    gameBoard.classList.remove(Player.X, Player.O);
    gameBoard.classList.add(currentPlayer);
}
function checkWin(player) {
    var isWin = win.some(function (ele) {
        var cell1 = cells[ele[0]];
        var cell2 = cells[ele[1]];
        var cell3 = cells[ele[2]];
        if (hasClass(cell1, player) && hasClass(cell2, player) && hasClass(cell3, player)) {
            return true;
        }
        return false;
    });
    return isWin;
}
function hasClass(el, name) {
    return el.classList.contains(name);
}
restart.addEventListener('click', restartGame, { once: true });
function restartGame() {
    message.style.display = 'none';
    board.classList.remove(Player.X, Player.O);
    board.classList.add(Player.X);
    cells.forEach(function (item) {
        var cell = item;
        cell.classList.remove(Player.X, Player.O);
        cell.removeEventListener('click', clickCell);
        cell.addEventListener('click', clickCell, { once: true });
    });
    steps = 0;
    currentPlayer = Player.X;
    restart.removeEventListener('click', restartGame);
    restart.addEventListener('click', restartGame, { once: true });
}
